/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __PRECACHER_H_LIB3D__
#define __PRECACHER_H_LIB3D__

#include <lib3d/node/Root.h>
#include <lib3d/node/Node.h>
#include <xml/Object.h>

namespace liba
{
namespace lib3d
{

class Precacher : private xml::ObjectANSI
{
	node::Root precache_root;
	std::map<filesystem::String, node::Node *> nodemap;
	std::vector<filesystem::String> models_on_level;
	bool on_a_level;	//when level ends - model will be unloaded
	bool precaching;	//state of the precacher - load or unload models file

	class NodeLoader : public xml::ObjectANSI
	{
		Precacher * pre;
		std::string name;
		filesystem::String scene_name;
	public:
		NodeLoader(Precacher * pre);
		virtual bool on_attribute(xml::Provider * prov, const std::string & name, const std::string & value);
		virtual bool on_end_node(xml::Provider * prov);
	};
	friend class NodeLoader;
	void create_precached_node(const std::string & name, const filesystem::String & scene_name);

public:
	Precacher();
	virtual ~Precacher();

	void precache(const filesystem::String & filename);
	void uncache(const filesystem::String & filename);

	node::Node * load_scene(const filesystem::String & filename, node::Node * parent);
	void start_new_level();
	void end_new_level();

	virtual bool on_node(xml::Provider * prov, const std::string & name);
};

} //namespace lib3d
} //namespace liba

#endif